package com.rock.upms.web.common.config;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.core.io.DefaultResourceLoader;
import com.google.common.collect.Maps;
import com.rock.common.util.PropertiesFileUtil;
import com.rock.common.util.StringUtil;

/**
 * upms web 配置文件
 * 
 * @author hotdog
 *
 */
public class WebConfig {

	private static WebConfig global = new WebConfig();
	private static Map<String, String> map = Maps.newHashMap();
	public static final String SHOW = "1";
	public static final String HIDE = "0";
	public static final String YES = "1";
	public static final String NO = "0";
	public static final String TRUE = "true";
	public static final String FALSE = "false";
	public static final String SESSION_MAC_ADDRESS = "machineaddress";
	public static final String TGT_KEY = "tgt";
	public static final long PDA_SESSION_TIMEOUT = 7200L;
	public static final String CAS_TICKET_ADMIN_WEB_SESSION_KEY = "CAS_TICKET_ADMIN_WEB:sessions-";
	public static final String CAS_TICKET_ADMIN_WEB_KEY = "CAS_TICKET_ADMIN_WEB:";
	public static final String CAS_USER_INFO_KEY = "CAS_USER_INFO:";
	public static Map<String, Object> dictMap = new ConcurrentHashMap();
	public static String ipAddress = "";

	public static WebConfig getInstance() {

		return global;
	}

	public static String getConfig(String key) {

		String value = (String) map.get(key);
		if (value == null) {
			value = PropertiesFileUtil.getInstance().get(key);
			map.put(key, value != null ? value : "");
		}
		return value;
	}

	public static int getPageSize() {

		return StringUtil.toInteger(getConfig("page.size"));
	}

	public static int getPageNavigatePages() {

		return StringUtil.toInteger(getConfig("page.navigate.pages"));
	}

	public static String getAppName() {

		return getConfig("app.name");
	}

	public static int getAesIterationCount() {

		return StringUtil.toInteger(getConfig("aes.iterationCount"));
	}

	public static int getAesKeySize() {

		return StringUtil.toInteger(getConfig("aes.keysize"));
	}

	public static String getAppCopyrightYear() {

		return getConfig("app.copyrightYear");
	}

	public static String getAppVersion() {

		return getConfig("app.version");
	}

	public static String getBasePath() {

		return getConfig("basePath");
	}

	public static String getAdminPath() {

		return getConfig("adminPath");
	}

	public static String getFrontPath() {

		return getConfig("frontPath");
	}

	public static String getUserSourcePath() {

		return getConfig("user.sourcePath");
	}

	public static String getWebMaxUploadSize() {

		return getConfig("web.maxUploadSize");
	}

	public static String getWebStaticFile() {

		return getConfig("web.staticFile");
	}

	public static String getSessionTimeout() {

		return getConfig("session.sessionTimeout");
	}

	public static String getSessionTimeoutClean() {
		return getConfig("session.sessionTimeoutClean");
	}

	public static String getUrlSuffix() {
		return getConfig("urlSuffix");
	}

	// redis 配置
	public static String getRedisIp() {
		return getConfig("redis.ip");
	}
	public static String getRedisPort() {
		return getConfig("redis.port");
	}
	public static String getRedisPassword() {
		return getConfig("redis.password");
	}
	public static String getRedisKeyPrefix() {
		return getConfig("redis.keyPrefix");
	}
	public static boolean getTestOnBorrow() {
		if ("true".equals(getConfig("redis.testOnBorrow"))) {
			return true;
		} else {
			return false;
		}
	}
	public static String getRedisMaxActive() {
		return getConfig("redis.maxActive");
	}
	public static String getRedisMaxIdle() {
		return getConfig("redis.maxIdle");
	}
	public static String getRedisMaxWait() {
		return getConfig("redis.maxWait");
	}
	public static String getRedisTimeout() {
		return getConfig("redis.timeout");
	}
	
	
	
	public static Object getConst(String field) {

		try {
			return WebConfig.class.getField(field).get(null);
		} catch (IllegalArgumentException | IllegalAccessException | NoSuchFieldException | SecurityException localIllegalArgumentException) {
		}
		return null;
	}

	public static String getProjectPath() {

		String projectPath = getConfig("projectPath");
		if (StringUtil.isNotBlank(projectPath)) {
			return projectPath;
		}
		try {
			File file = new DefaultResourceLoader().getResource("").getFile();
			if (file != null) {
				for (;;) {
					File f = new File(file.getPath() + File.separator + "src" + File.separator + "main");
					if ((f == null) || (f.exists())) {
						break;
					}
					if (file.getParentFile() == null) {
						break;
					}
					file = file.getParentFile();
				}
				projectPath = file.toString();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return projectPath;
	}

	public static void main(String[] args) {

		System.out.println(WebConfig.getAppName());
		System.out.println(WebConfig.getAppCopyrightYear());
		System.out.println(WebConfig.getAppVersion());
		System.out.println("WebGlobal.getBasePath()" + WebConfig.getBasePath());
		System.out.println(WebConfig.getAdminPath());
		System.out.println(WebConfig.getFrontPath());
		System.out.println(WebConfig.getUserSourcePath());
		System.out.println(WebConfig.getWebMaxUploadSize());
		System.out.println(WebConfig.getWebStaticFile());
		System.out.println(WebConfig.getSessionTimeout());
		System.out.println(WebConfig.getSessionTimeoutClean());
		System.out.println("------------------------------------------");
		System.out.println(WebConfig.getAesKeySize());
	}
}
